package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.anno.NeedLogin;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.exception.CustomerException;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.base.service.IRealAuthService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.domain.PaymentSchedule;
import cn.wolfcode.p2p.business.query.PaymentScheduleQuery;
import cn.wolfcode.p2p.business.service.IBidRequestService;
import cn.wolfcode.p2p.business.service.IPaymentScheduleService;
import cn.wolfcode.p2p.util.Constants;
import cn.wolfcode.p2p.util.JsonResult;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 借款逻辑
 */
@Controller
public class BorrowController {

    @Autowired
    private IAccountService accountService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IBidRequestService bidRequestService;
    @Autowired
    private IPaymentScheduleService paymentScheduleService;

    /**
     * 借款页面
     * @param model
     * @return
     */
    @RequestMapping("borrow")
    public String borrow(Model model){
        LoginInfo currentUser = UserContext.getCurrentUser();
        //登录后,跳到借款页面borrow.ftl
        if (currentUser != null) {
            model.addAttribute("account", accountService.getById(currentUser.getId()));
            model.addAttribute("userinfo", userInfoService.getById(currentUser.getId()));
            return "borrow/borrow";
        }
        return "redirect:/borrow.html";
    }

    /**
     * 借款申请页面
     * @param model
     * @return
     */
    @RequestMapping("borrowInfo")
    @NeedLogin
    public String borrowInfo(Model model){
        UserInfo userInfo = userInfoService.getById(UserContext.getCurrentUser().getId());
        if (userInfo.hasBorrowInProcess()) {
            return "borrow/borrow_apply_result";
        }
        //最小借款金额
        model.addAttribute("minBidRequestAmount", Constants.BORROW_MIN_AMOUNT);
        //最小投标金额
        model.addAttribute("minBidAmount", Constants.BID_MIN_AMOUNT);

        //账户
        model.addAttribute(("account"), accountService.getById(UserContext.getCurrentUser().getId()));
       //最小利率
        model.addAttribute("minRate", Constants.BORROW_MIN_RATE);
        //最大利率
        model.addAttribute("maxRate", Constants.BORROW_MAX_RATE);
        return "borrow/borrow_apply";
    }
    /**
     * 借款申请
     */
    @RequestMapping("borrow_apply")
    @NeedLogin
    @ResponseBody
    public JsonResult borrowApply(BidRequest bidRequest){
        JsonResult jsonResult = new JsonResult();
        try {
            bidRequestService.apply(bidRequest);
        } catch (CustomerException e) {
            jsonResult.setMessage(e.getMessage());
            throw new CustomerException(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("系统繁忙中,请稍后再试");
        }
        return jsonResult;
    }

    /**
     * 还款列表
     */
    @RequestMapping("borrowBidReturn_list")
    @NeedLogin
    public String borrowBidReturnList(Model model, @ModelAttribute("qo") PaymentScheduleQuery qo){
        qo.setBorrowUserId(UserContext.getCurrentUser().getId());
        model.addAttribute("pageInfo", paymentScheduleService.query(qo));
        //还款人账户
        model.addAttribute("account", accountService.getById(UserContext.getCurrentUser().getId()));
        return "borrow/returnmoney_list";
    }

    /**
     * 执行还款
     * id 还款计划的id
     */
    @RequestMapping("returnMoney")
    @ResponseBody
    public JsonResult returnMoney(Long id){
        JsonResult jsonResult = new JsonResult();
        try {
            bidRequestService.returnMoney(id);
        } catch (CustomerException e) {
            jsonResult.setMessage(e.getMessage());
            throw new CustomerException(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("系统繁忙中,请稍后再试");
        }
        return jsonResult;
    }
}
